//
//  S1mpl3AlbumTableViewCell.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "S1mpl3AlbumTableViewCell.h"
#import "FitHeader.h"
#import "UIImageView+WebCache.h"

@implementation S1mpl3AlbumTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView{
    _coverPathImageView = [[UIImageView alloc]init];
    
    //        _coverPathImageView.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_coverPathImageView];
    
    _titleTopLabel = [[BaseLabel alloc]init];
    [_titleTopLabel setFontSize:16];
    _titleTopLabel.numberOfLines = 0;
    //    _titleTopLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_titleTopLabel];
    
    _titleTopSubLabel = [[BaseLabel alloc]init];
    [_titleTopSubLabel setFontSize:14];
    //    _titleTopLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_titleTopSubLabel];
    
    _titleCenterLabel = [[BaseLabel alloc]init];
    [_titleCenterLabel setFontSize:14];
    _titleCenterLabel.textColor = [UIColor lightGrayColor];
    //    _titleCenterLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_titleCenterLabel];
    
    _backView = [[BaseView alloc] init];
    [self.contentView addSubview:_backView];
    
    _playImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_playImageView];
    
    _loveImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_loveImageView];
    
    _remarkImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_remarkImageView];
    
    _timeImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_timeImageView];
    
    _downloadImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_downloadImageView];
    
    _titleBottomLeftLabel = [[BaseLabel alloc]init];
    [_titleBottomLeftLabel setFontSize:12];
    _titleBottomLeftLabel.textColor = [UIColor lightGrayColor];
    //    _titleBottomLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_titleBottomLeftLabel];
    
    _titleBottomMidLeftLabel = [[BaseLabel alloc]init];
    [_titleBottomMidLeftLabel setFontSize:12];
    _titleBottomMidLeftLabel.textColor = [UIColor lightGrayColor];
    //    _titleBottomLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_titleBottomMidLeftLabel];
    
    _titleBottomMidRightLabel = [[BaseLabel alloc]init];
    [_titleBottomMidRightLabel setFontSize:12];
    _titleBottomMidRightLabel.textColor = [UIColor lightGrayColor];
    //    _titleBottomLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_titleBottomMidRightLabel];
    
    _titleBottomRightLabel = [[BaseLabel alloc]init];
    [_titleBottomRightLabel setFontSize:12];
    _titleBottomRightLabel.textColor = [UIColor lightGrayColor];
    //    _titleBottomLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_titleBottomRightLabel];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    _coverPathImageView.frame = CGRectMake(10 * FITWidth, 10 * FITHeight, 100 * FITWidth, 100 * FITHeight);
    _coverPathImageView.layer.masksToBounds = YES;
    _coverPathImageView.layer.cornerRadius = 50 * FITHeight;
    
    _titleTopLabel.frame = CGRectMake(_coverPathImageView.frame.origin.x + _coverPathImageView.frame.size.width + 10 * FITWidth, _coverPathImageView.frame.origin.y + 5 * FITHeight, 200 * FITWidth, 60 * FITHeight);
    
    _titleTopSubLabel.frame = CGRectMake(_titleTopLabel.frame.origin.x + _titleTopLabel.frame.size.width, 15 * FITHeight, 55 * FITWidth, 20 * FITHeight);
    [_titleTopSubLabel setFontSize:12];
    _titleTopSubLabel.textAlignment = 1;
    _titleTopSubLabel.textColor = [UIColor lightGrayColor];
    
    _titleCenterLabel.frame = CGRectMake(_titleTopLabel.frame.origin.x, _titleTopLabel.frame.origin.y + _titleTopLabel.frame.size.height, 200 * FITWidth, 25 * FITHeight);
    
    _playImageView.frame = CGRectMake(_titleCenterLabel.frame.origin.x, _titleCenterLabel.frame.origin.y + _titleCenterLabel.frame.size.height + 4 * FITHeight, 8 * FITHeight, 8 * FITHeight);
    
    _titleBottomLeftLabel.frame = CGRectMake(_titleCenterLabel.frame.origin.x + 8 * FITWidth, _titleCenterLabel.frame.origin.y + _titleCenterLabel.frame.size.height, 40 * FITWidth, 15 * FITHeight);
    
    _loveImageView.frame = CGRectMake(_titleBottomLeftLabel.frame.origin.x + _titleBottomLeftLabel.frame.size.width, _playImageView.frame.origin.y, 8 * FITHeight, 8 * FITHeight);
    
    _titleBottomMidLeftLabel.frame = CGRectMake(_loveImageView.frame.origin.x + 8 * FITWidth, _titleBottomLeftLabel.frame.origin.y, 35 * FITWidth, _titleBottomLeftLabel.frame.size.height);
    
    _remarkImageView.frame =CGRectMake(_titleBottomMidLeftLabel.frame.origin.x + _titleBottomMidLeftLabel.frame.size.width, _playImageView.frame.origin.y, 8 * FITHeight, 8 * FITHeight);
    
    _titleBottomMidRightLabel.frame = CGRectMake(_remarkImageView.frame.origin.x + 8 * FITWidth, _titleBottomLeftLabel.frame.origin.y, _titleBottomMidLeftLabel.frame.size.width, _titleBottomLeftLabel.frame.size.height);
    
    _timeImageView.frame = CGRectMake(_titleBottomMidRightLabel.frame.origin.x + _titleBottomMidLeftLabel.frame.size.width, _playImageView.frame.origin.y, 8 * FITHeight, 8 * FITHeight);
    
    _titleBottomRightLabel.frame = CGRectMake(_timeImageView.frame.origin.x + 8 * FITWidth, _titleBottomLeftLabel.frame.origin.y, _titleBottomMidLeftLabel.frame.size.width, _titleBottomLeftLabel.frame.size.height);

    _backView.frame = CGRectMake(340 * FITWidth, 100 * FITHeight, 24 * FITHeight, 24 * FITHeight);
    _backView.layer.masksToBounds = YES;
    _backView.layer.cornerRadius = 12 * FITHeight;
    _backView.layer.borderWidth = 0.5;
    _backView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    _downloadImageView.frame = CGRectMake(340 * FITWidth, 100 * FITHeight, 16 * FITHeight, 16 * FITHeight);
    _downloadImageView.center = _backView.center;
    
    _playImageView.image = [UIImage imageNamed:@"S1mpl3播放"];
    _loveImageView.image = [UIImage imageNamed:@"S1mpl3收藏"];
    _remarkImageView.image = [UIImage imageNamed:@"S1mpl3评论"];
    _timeImageView.image = [UIImage imageNamed:@"S1mpl3时钟"];
    _downloadImageView.image = [UIImage imageNamed:@"S1mpl3下载big"];
    
}

-(void)setDic:(NSDictionary *)dic{
    [_coverPathImageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"coverMiddle"]] placeholderImage:[UIImage imageNamed:@""]];
    _titleTopLabel.text = [dic objectForKey:@"title"];
    _titleTopSubLabel.text = [dic objectForKey:@""];
    _titleCenterLabel.text = [NSString stringWithFormat:@"by %@", [dic objectForKey:@"nickname"]];
    _titleBottomLeftLabel.text = [NSString stringWithFormat:@"%.1f万", [[dic objectForKey:@"playtimes"] floatValue] / 10000];
    _titleBottomMidLeftLabel.text = [NSString stringWithFormat:@"%@", [dic objectForKey:@"likes"]];
    _titleBottomMidRightLabel.text = [NSString stringWithFormat:@"%@", [dic objectForKey:@"comments"]];
    NSInteger time = [[dic objectForKey:@"duration"] integerValue];
    NSInteger hour = time / 60;
    NSInteger minute = time - hour * 60;
    if (minute < 10) {
        _titleBottomRightLabel.text = [NSString stringWithFormat:@"%ld:0%ld", hour, minute];
    }else
        _titleBottomRightLabel.text = [NSString stringWithFormat:@"%ld:%ld", hour, minute];
    
    
    
    NSInteger date = [[dic objectForKey:@"createdAt"] integerValue] / 1000;
    NSDate *dateString = [NSDate dateWithTimeIntervalSince1970:date];
    _titleTopSubLabel.text = [S1mpl3AlbumTableViewCell compareCurrentTime:dateString];
}

+(NSString *) compareCurrentTime:(NSDate*) compareDate
//
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
